package org.com.Controller;

import Entity.Blog;
import Entity.OrderVoucher;
import Entity.Voucher;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.com.Entity.User;
import org.com.FeignClient.BlogFeign;
import org.com.FeignClient.OrderFeign;
import org.com.FeignClient.UserFeign;
import org.com.Service.IBlogService;
import org.com.Service.IOrderVoucherService;
import org.com.Service.IVoucherService;
import org.com.Uitls.ExcelUtils;
import org.com.Uitls.MQexcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @Autowired
    private BlogFeign blogFeign;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private UserFeign userFeign;

    @PostMapping("/upload")
//    public JSONArray upload(MultipartFile  file) throws Exception {
//        JSONArray jsonArray = ExcelUtils.readMultipartFile(file);
//     log.info("jsonArray:{}",jsonArray);
//        return jsonArray;

    public void upload(MultipartFile file) throws Exception {
        List<org.com.Entity.User> users = ExcelUtils.readMultipartFile(file, org.com.Entity.User.class);
        log.info("users:{}", users);
        for (User user : users) {
            System.out.println(user.toString());
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        List<Object> list = Arrays.asList("姓名", "电话", "年龄");

        List<Object> arrayList = new ArrayList<>();
        arrayList.add("渐思南");
        arrayList.add("123456");
        arrayList.add(18);

        List<Object> arrayList1 = new ArrayList<>();

        arrayList1.add("王大锤");
        arrayList1.add("123456");
        arrayList1.add(18);

        List<List<Object>> objectArrayList = new ArrayList<>();
        objectArrayList.add(list);
        objectArrayList.add(arrayList);
        objectArrayList.add(arrayList1);


        System.out.println("数据" + objectArrayList);
        log.info("导出成功:{}", objectArrayList);
        ExcelUtils.export(response, "用户信息", objectArrayList);


    }

    @GetMapping("/export/{type}")
    public void exporttype(@PathVariable("type") String type, HttpServletResponse response) throws Exception {
        switch (type) {
            case "users":
                List<Entity.User> userList = userFeign.list();
                MQexcelUtils.exportExcel(response, userList, "用户信息", "用户表", Entity.User.class);
                log.info("导出成功:{}", userList);
                break;

            case "blogs":
                List<Blog> blogList = blogFeign.list();
                MQexcelUtils.exportExcel(response, blogList, "博客信息", "博客表", Blog.class);
                log.info("导出成功:{}", blogList);
                break;
            case "orders":
                List<OrderVoucher> orderList = orderFeign.list();
                MQexcelUtils.exportExcel(response, orderList, "订单信息", "订单表", OrderVoucher.class);
                log.info("导出成功:{}", orderList);
                break;

            default:
                log.warn("不支持的导出类型: {}", type);
                throw new IllegalArgumentException("不支持的导出类型: " + type);
        }
        // 注意：方法返回类型为void，不需要返回任何内容，避免污染响应流
    }


}

